package com.zhao.service.serviceImpl;

import com.zhao.Others.FileOperationObject;
import com.zhao.dao.DepartmentDao;
import com.zhao.dao.MajorDao;
import com.zhao.dao.StudentDao;
import com.zhao.dao.UniversityDao;
import com.zhao.pojo.*;
import com.zhao.service.StudentService;
import org.apache.logging.log4j.core.pattern.AbstractStyleNameConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {

    @Autowired
    UniversityDao universityDao;

    @Autowired
    DepartmentDao departmentDao;

    @Autowired
    MajorDao majorDao;

    @Autowired
    StudentDao studentDao;

    @Override
    public String getStudentNum(String universityID,HttpSession session) {
        List<Student> studentList = studentDao.findAllByUniversityID(universityID);
        session.setAttribute("studentNum",studentList.size());
        return "success";
    }

    @Override
    public StudentImpl getStudentImpl(Student student) {

        StudentImpl student1 = new StudentImpl();
        student1.setStudentID(student.getStudentID());
        student1.setName(student.getName());
        student1.setImage(student.getImage());
        student1.setEmial(student.getEmail());
        student1.setPhoneNumber(student.getPhoneNumber());
        student1.setNickName(student.getNickName());
        student1.setGender(student.getGender());
        student1.setPassword(student.getPassWord());

        student1.setUniversityID(student.getUniversityID());
        University university = universityDao.findByID(student.getUniversityID());
        if(university!=null){
            student1.setUniversityName(university.getUniversityName());
        }


        student1.setDepartmentID(student.getDepartmentID());
        Department department = departmentDao.findByID(student1.getDepartmentID(),student.getUniversityID());
        if(department!=null){
            student1.setDepartmentName(department.getDepartmentName());
        }

        Major major = majorDao.findByID(student.getMajorID(),student.getUniversityID());
        if(major != null){
            student1.setMajorID(student.getMajorID());
            student1.setMajorName(major.getMajorName());
        }

        student1.setStuClass(student.getStuClass());
        student1.setClassName(student1.getMajorName()+student.getStuClass());
        System.out.println("service studentImpl");

        student1.setEbickAmount(student.getEbickAmount());

        return student1;
    }

    @Override
    public String finishStudentInfo(StudentImpl studentImpl, MultipartFile image , HttpSession session) {

        Student student = (Student)session.getAttribute("student");
        System.out.println(student);

        ///完善信息后  标记为注册完成
        student.setIsRegisted("1");

        student.setEmail(studentImpl.getEmial());
        student.setNickName(studentImpl.getNickName());
        student.setPhoneNumber(studentImpl.getPhoneNumber());

        //图片保存到服务器
        String visitPath = FileOperationObject.savePicture(image,"userimage",student.getUniversityID(),student.getStudentID());
        if(visitPath==null){
            return "error";//保存失败
        }

        //图片存储访问路径
        student.setImage(visitPath);

        System.out.println(student);
        int res = studentDao.updateStudent(student);
        if(res==1){
            return "success";
        }
        return "error";
    }

    ////////////////////////  admin  //////////////////////////////

    @Override
    public String adminGetAllStudents(Admin admin,HttpSession session) {

        List<Student> studentList = studentDao.findAllByUniversityID(admin.getUniversityID());

        System.out.println("找到 "+studentList.size());

        List<StudentImpl> studentImplList = new ArrayList<>();
        for(Student student : studentList){
            StudentImpl student1 = getStudentImpl(student);
            studentImplList.add(student1);
        }

        session.setAttribute("studentImplList",studentImplList);

        return "success";
    }

    @Override
    public String deleteOneStudent(Admin admin, String studentID) {

        int res = studentDao.deleteByStudentID(studentID,admin.getUniversityID());

        if(res!=1){
            return "error";
        }

        return "success";
    }

    @Override
    public String adminAddOneStudent(StudentImpl studentImpl, HttpSession session) {

        Student student1 = studentDao.findByStudentID(studentImpl.getStudentID(),studentImpl.getUniversityID());
        if(student1!=null){
            return "ID已经存在,添加失败";
        }

        System.out.println(studentImpl);

        Student student = new Student();
        student.setUniversityID(studentImpl.getUniversityID());
        student.setDepartmentID(studentImpl.getDepartmentID());
        student.setStuClass(studentImpl.getStuClass());
        student.setMajorID(studentImpl.getMajorID());
        student.setEbickAmount(0);
        student.setPassWord("123");
        student.setGender(studentImpl.getGender());
        student.setName(studentImpl.getName());
        student.setStudentID(studentImpl.getStudentID());
        student.setIsRegisted("0");
        System.out.println(student);
        int res = studentDao.addStudent(student);
        if(res != 1){
            return "添加失败，更新数据库错误";
        }
        return "success";
    }

    @Override
    public String adminGetOneStudent(Admin admin,String studentID, HttpSession session) {

        Student student = studentDao.findByStudentID(studentID,admin.getUniversityID());
        System.out.println(student);
        if(student == null){
            return "学号错误";
        }
        StudentImpl student1 = getStudentImpl(student);
        System.out.println(student1);
        session.setAttribute("adminSeeOneStudent",student1);

        return "success";
    }

    @Override
    public String adminUpdateStudent(Admin admin,String studentID, String name, String department, String major, String stuClass, String password,
                                     String email,String phoneNumber,HttpSession session) {

        Student student = studentDao.findByStudentID(studentID,admin.getUniversityID());
        //System.out.println(student);
        student.setName(name);
        student.setStuClass(stuClass);
        student.setPassWord(password);
        student.setEmail(email);
        student.setPhoneNumber(phoneNumber);

        Department department1 = departmentDao.findByName(department,admin.getUniversityID());
        if(department1 == null){
            return "学院名称错误";
        }
        student.setDepartmentID(department1.getDepartmentID());

        Major major1 = majorDao.findByName(major,admin.getUniversityID());
        if(major1 == null){
            return "专业名称错误";
        }
        student.setMajorID(major1.getMajorID());

        //System.out.println(student);
        int res = studentDao.updateStudent(student);
        if(res != 1){
            return "更新失败";
        }

        return "success";
    }
}
